package com.ply.service.impl;

import com.ply.entity.pojo.Manager;
import com.ply.entity.pojo.Project;
import com.ply.entity.pojo.User;


import com.ply.entity.pojo.VoteOption;
import com.ply.entity.vo.ProjectAndOptionSum;
import com.ply.entity.vo.Result;
import com.ply.mapper.ManageMapper;
import com.ply.mapper.ProjectMapper;
import com.ply.mapper.UserMapper;
import com.ply.service.ManageService;

import com.ply.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.html.Option;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class ManageServiceImpl implements ManageService {
    Manager manager = new Manager();

    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    ManageMapper manageMapper;

    @Autowired
    ProjectServiceImpl projectService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserMapper userMapper;


    /**
     * @return 所有用户的信息
     * @Author hai jun
     * 查询所有用户的信息
     */
    public Result<?> queryAllUser() {
        Result list = userService.queryUserAll();
        return Result.ok(list);
    }

    /**
     * 查询所有未审核的项目
     */
    public Result<?> queryProjectAllByStatus(Integer status) {
        List<Project> list = projectMapper.queryProjectAllByStatus(status);
        return Result.ok(list);
    }


    /**
     * 投票创建审核
     *
     * @param projectId 项目id
     * @param isTrue    审核 成功 还是 失败 2成功 5失败
     */

    @Transactional
    public Result<?> checkVote(Integer projectId, int isTrue, int adminId) {
        log.debug("adminId:{}", adminId);
//        log.info("信息:{}",projectId,isTrue,adminId);
        Result res1 = projectService.addProjectAuditId(projectId, adminId);
        Result res2 = projectService.updateProjectStatus(projectId, isTrue);

        if (!res1.isTrue()) return res1;
        if (!res2.isTrue()) return res2;

        if (isTrue == 5) {
            Integer id = (Integer) res2.getDate();
            User user = userMapper.queryUserByUserId(id);
            Integer remainVoteSum = user.getRemainVoteSum();
            userService.addRemainVote(id, remainVoteSum + 1);
            return Result.ok("审核成功");
        }
        return Result.ok("审核成功");

    }

    /**
     * 审核登入
     *
     * @param name 用户名
     * @param pwd  密码
     * @return
     */
    public Result login(String name, String pwd) {
        Manager manager = manageMapper.queryManageByName(name);
        if (manager == null) {
            return Result.fail("用户名不存在");
        }
        if (!(manager.getPasswd().equals(pwd))) {
            return Result.fail("密码错误");
        }
//        返回数据中将密码更换
        manager.setPasswd("00");
        //将用户存进Map中方便获取Map中的

        return Result.ok("用户名密码正确", manager);
    }


    /**
     * 查询一个项目的所有投票记录
     *
     * @param projectId 所需要查询的项目id
     * @return
     */
    public Result queryVoteRecord(int projectId) {
        Result list = projectService.selectProjectNameAndOptionNameAndSum(projectId);
        return Result.ok(list);
    }


    /**
     * 查询等级 通过用户 id
     *
     * @param userId 用户id
     * @return 用户等级
     */
    public Result queryLevelByUserId(int userId) {
        Result result = userService.queryUserAll();
        List<User> users = (List<User>) result.getDate();

        for (User user : users) {
            if (userId == user.getId()) {
                return Result.ok(user.getLevelId());
            }
        }

        return Result.fail("请输入正确的参数");
    }


    /**
     * 查询 所有 的 项目  选项
     *
     * @param pageIndex 当前页数
     * @param pageSum   每页条数
     * @return
     */

    public Result queryAllProject(Integer pageIndex, Integer pageSum) {
        Result list = userService.queryAllProject(pageIndex, pageSum);
        return Result.ok(list);
    }


    /**
     * 查询我的创建项目 所有
     *
     * @return List集合 里面 全是项目
     */
    public Result queryMyProject(Integer pageIndex, Integer pageSum, Integer userId) {
        Result list = userService.queryMyProject(pageIndex, pageSum, userId);
        return list;
    }


    /**
     * 查询 所有用户的流水 (分页)
     *
     * @return
     */
    public Result<?> queryAllByUserRecode() {
        Result list = userService.queryAllByUserRecode();
        return list;
    }
}

